package game.Weapons;

import game.Configuration;
import game.Gamemode.TeamDeathmatchManager;
import game.Player.Player;
import game.Scene;
import game.Sprites.Sprite;
import game.Utils.GraphicsUtils;
import game.Vector3;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;

import javafx.scene.image.Image;
import java.awt.geom.Rectangle2D;
import java.util.List;

public class DroppedWeapon extends Sprite {
    private Weapon_info weapon;
    private Image img;
    private double size;
    private double sizeX;
    private double sizeY;

    // Constructor
    public DroppedWeapon(Weapon_info weapon, double size, Vector3 position, double rotation, Color color) throws Exception {
        super(position, rotation, color);

        setZ(0.0);
        
        this.weapon = weapon;
        this.img = weapon.getShopImage2();
        this.size = size;
        this.sizeX = img.getWidth() * 0.02;
        this.sizeY = img.getHeight() * 0.02;
    }

    @Override
    public void draw(GraphicsContext gd, Vector3 screenPos, double squareRes) {
        // Draw shadow
        GraphicsUtils.drawSpriteShadow(gd, screenPos, 1.0, 1.0, squareRes);

        // Draw weapon sprite
        GraphicsUtils.drawRotatedImage(gd, img,rotation,(int) screenPos.x - (int) (squareRes * sizeX / 2),
                (int) screenPos.y - (int) (squareRes * sizeY / 2),(int) (squareRes * sizeX), (int) (squareRes * sizeY));
    }

    @Override
    public Rectangle2D.Double getBound() {
        return new Rectangle2D.Double(this.position.x - sizeX/2, this.position.y - sizeX/2, sizeX, sizeY);
    }

    @Override
    public void update(double deltaTime) {
        // si myPlayer esta congelado destruir objeto
        if(Player.myPlayer.getFreeze()) {
            this.destroy();
        }
        // Comprobar para cada uno de los jugadores si alguno quiere coger el arma
        List<Player> playerList = TeamDeathmatchManager.getInstance().getPlayerList();
        double minDist = 0.0;
        boolean isPicking;
        for(int i=0; i<playerList.size(); i++) {
            Player pl = playerList.get(i);
            if(!pl.isAlive())
                continue;

            Vector3 posPlayer = pl.getPosition();
            posPlayer.z = 0.0;

            Vector3 posWeapon = this.getPosition();
            posWeapon.z = 0.0;

            double distance2d = Vector3.distance(posPlayer, posWeapon);

            if(distance2d < 0.3){
                // Add pickup text to camera if myplayer
                if(pl == Player.myPlayer && Scene.getInstance()!= null && Scene.getInstance().getCamera() != null){
                    KeyCode key = KeyCode.getKeyCode(Configuration.getPickUp());
                    Scene.getInstance().getCamera().addCenterMessage("Press " + key.getName() + " to pickup " + weapon.getName());
                }

                // Pickup weapon by player
                isPicking = pl.isPicking();
                if(isPicking) {
                    Weapon_info newDropped = pl.interchangeWeapon(weapon);
                    interchange(newDropped);
                    break;
                }
            }
            else if(Math.abs(posPlayer.x - position.x) < 1 && Math.abs(posPlayer.y - position.y) < 1 && playerList.get(i).primary_weapon == null &&
                    playerList.get(i).isAlive()) {
                Weapon_info newDropped = playerList.get(i).interchangeWeapon(weapon);
                interchange(newDropped);

            }
        }
    }

    public void interchange(Weapon_info newWeapon) {
        if(newWeapon == null) {
            this.destroy();
        }
        else {
            this.weapon = newWeapon;
            this.img = weapon.getShopImage2();
            this.sizeX = img.getWidth() * 0.02;
            this.sizeY = img.getHeight() * 0.02;
        }
    }
}